package org.ghost.http.testkit.controller;

import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.ghost.http.testkit.dto.ConfigSystemEnvDTO;
import org.ghost.http.testkit.dto.ResponseVO;
import org.ghost.http.testkit.entity.ConfigSystemEnv;
import org.ghost.http.testkit.service.IConfigSystemEnvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2018-05-02
 */
@RestController
@RequestMapping("/configSystemEnv")
public class ConfigSystemEnvController extends BaseController {
    @Autowired
    private IConfigSystemEnvService service;

    /**
     * 实体查询
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResponseVO search(@PathVariable Long id) {
        return getFromData(service.selectById(id));
    }

    /**
     * 实体删除
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResponseVO del(@PathVariable Long id) {
        service.deleteById(id);
        return getSuccess();
    }

    /**
     * 实体添加
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseVO add(@RequestBody ConfigSystemEnv entity) {
        service.insert(entity);
        return getSuccess();
    }


    /**
     * 实体修改
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResponseVO update(@PathVariable Long id, @RequestBody ConfigSystemEnv entity) {
        entity.setId(id);
        service.updateById(entity);
        return getSuccess();
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseVO selectPage(Page<ConfigSystemEnv> page) {
        page = page == null ? new Page<ConfigSystemEnv>(1, 10) : page;

        ConfigSystemEnvDTO reqDTO = new ConfigSystemEnvDTO();
        if (MapUtils.isNotEmpty(page.getCondition())) {
            reqDTO.setEnvName(MapUtils.getString(page.getCondition(), "envName", ""));
            reqDTO.setSysName(MapUtils.getString(page.getCondition(), "sysName", ""));
            reqDTO.setEnvId(MapUtils.getLong(page.getCondition(), "envId"));
            reqDTO.setSysId(MapUtils.getLong(page.getCondition(), "sysId"));
        }

        List<ConfigSystemEnvDTO> result = service.selectConfigSystemEnv(page, reqDTO);
        if (CollectionUtils.isNotEmpty(result)) {
            Page<ConfigSystemEnvDTO> pageResult = new Page<ConfigSystemEnvDTO>(page.getCurrent(), page.getSize());
            pageResult.setTotal(page.getTotal());
            pageResult.setRecords(result);

            return getFromData(pageResult);
        }
        return getFromData(page);
    }
}
